﻿<?php

global $config;
if (isset($_GET['act'])) $act = $_GET['act'];

$list_url = "index.php?mod=hotel&act=list";
$delete_url = $list_url . "&task=delete&id=";
$view_url = "index.php?mod=hotel&act=view&id=";
?>
<?php

if ($act == "list") {
?>
    <h3 class='action-title'>Danh sách khách sạn</h3>
    <?php
    if (isset($_GET['task']) && $_GET['task'] == 'delete') {
        if (isset($_GET['id'])) $id = $_GET['id'];
        $sql = "delete from dnhotel where id = $id";
		$pg = new PgSQL();
		$pg->connect();
        if ($pg->query($sql)) notice("Đả xóa khách sạn.");
        else notice("Lổi: chưa cập nhật được dữ liệu.");
		$pg->disconnect();
    }
    ?>
    <div id="list-posts">
        <div class="header-list">
            <label class="header-username">Tên Khách Sạn</label>
            <label class="header-email">Địa Chỉ</label>
            <label class="header-edit">Xem</label>
            <label class="header-delete">Xóa</label>
            <div class="clear"></div>
        </div>
        <?php
            $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
            $limit = $config['news_per_page']; //10
            $start = $start = $limit * ($page - 1);
            $sql = "select * from dnhotel order by id desc";// limit $limit offset $start";
			$pg = new PgSQL();
			$pg->connect();
			$data = $pg->query($sql);
            //$data = $db->get_data($db->query($sql));
            
            if ($pg->numberRows() != 0) :
            while ($row = pg_fetch_object($data)) {
        ?>
        <div class="row-post">
            <label class="header-username"><?php echo $row->name; ?></label>
            <label class="header-email"><?php echo $row->address ?></label>
            <label class="header-edit"><a href="<?php echo $view_url.$row->id ?>" class="edit-post" title="Sửa">Sửa</a></label>
            <label class="header-delete"><a href="<?php echo $delete_url.$row->id; ?>" class="delete-post" title="Xóa">Xóa</a></label>
            <div class="clear"></div>
        </div>
        <?php
            }
            endif;
            ViewPages($config['news_per_page'], "contact", "contact");
			$pg->disconnect();
        ?>
    </div>

<?php
} else if ($act == "view") {
    echo "<h3 class='action-title'>Sửa thông tin khách sạn</h3>";
	
	$id = (isset($_GET['id'])) ? $_GET['id'] : 0;
	
	if (isset($_POST['submit'])) {
		$name = "'".$_POST['name']."'";
		$address = "'".$_POST['address']."'";
		$phone = "'".$_POST['phone']."'";
		$website = "'".$_POST['website']."'";
		$district = "'".$_POST['district']."'";
		$rooms = $_POST['rooms'];
		$star = $_POST['star'];
		$sql = "UPDATE dnhotel SET name=".$name.", address=".$address.", phone=".$phone.", website=".$website.", district=".$district.", rooms=".$rooms.", star=".$star." 
		WHERE id=".$id;
		$pg = new PgSQL();
		$pg->connect();
		if ($pg->query($sql)) notice("Đã cập nhật thông tin khách sạn.");
        else notice("Lỗi: chưa cập nhật được.");
		$pg->disconnect();
		
	}
	
	$sql = "select * from dnhotel where id = ".$id;
	$pg = new PgSQL();
	$pg->connect();
	$data = $pg->query($sql);
    //$data = $db->fetch_array($db->query($sql));
    if ($row = pg_fetch_object($data)){
?>
	<form id="update-hotel" method="post" action="">
		<p class="contact-row">
			<label class="contact-label">Tên Khách Sạn</label>
			<input name="name" type="text" class="required" style="width: 300px;" value="<?php echo $row->name ?>" />
		</p>
		<p class="contact-row">
			<label class="contact-label">Địa chỉ</label>
			<input name="address" type="text" class="required" style="width: 300px;" value="<?php echo $row->address ?>" />
		</p>
		<p class="contact-row">
			<label class="contact-label">Điện thoại</label>
			<input name="phone" type="text" class="contact-input" value="<?php echo $row->phone ?>" />
		</p>
		<p class="contact-row">
			<label class="contact-label">Điện thoại</label>
			<input name="website" type="text" class="contact-input" value="<?php echo $row->website ?>" />
		</p>
		<p class="contact-row">
			<label class="contact-label">Quận</label>
			<input name="district" type="text" class="contact-input" value="<?php echo $row->district ?>" />
		</p>
		<p class="contact-row">
			<label class="contact-label">Số phòng</label>
			<input name="rooms" type="text" class="contact-input" value="<?php echo $row->rooms ?>" />
		</p>
		<p class="contact-row">
			<label class="contact-label">Số sao</label>
			<input name="star" type="text" class="contact-input" value="<?php echo $row->star ?>" />
		</p>
		<input type="submit" name="submit" class="button" value="Cập nhật" />
        <input type="reset" name="reset" class="button" value="Hủy" />
	</form>
	<script>
	$("#update-hotel").validate();
	</script>
<?php
	}
	$pg->disconnect();
}
?>